package com.guo.core.framework.chain;

import com.guo.core.framework.chain.checker.Checker;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * 检查链
 * @param <T> 待检查的参数
 */
public class CheckChain<T> implements Chain<Checker<T>, CheckChain<T>> {

    private final List<Checker<T>> chain = new ArrayList<>();

    @Override
    public CheckChain<T> addChain(Checker<T> element) {
        chain.add(element);
        return this;
    }

    public void check(T req) {
        Assert.notEmpty(chain, "检查链节点不能为空。");
        chain.forEach(check -> check.check(req));
    }

}
